package com.us.usws.site.ws.entity.video;

import java.util.ArrayList;
import java.util.List;

import com.us.usws.biz.VideoRateStartWorker;
import com.us.usws.biz.WSResourceURIReplacer;
import com.us.usws.model.domain.base.BaseVideoCat;
import com.us.usws.model.domain.system.User;
import com.us.usws.model.domain.video.Video;
import com.us.usws.model.domain.video.VideoComment;
import com.us.usws.model.domain.video.VideoExtattr;
import com.us.usws.model.domain.video.VideoFile;
import com.us.usws.model.flat.base.DOBaseVideoCat;
import com.us.usws.model.flat.system.DOUser;
import com.us.usws.model.flat.video.DOVideo;
import com.us.usws.service.ro.UserROService;
import com.us.usws.service.ro.VideoFileROService;
import com.us.usws.service.ro.VideoPlayLogROService;
import com.us.usws.site.ws.entity.video.videocomment.WSVideoCommentDetail;
import com.us.usws.site.ws.entity.video.videocomment.WSVideoCommentUser;
import com.us.usws.site.ws.entity.video.videoinfo.WSVideoInfoComment;
import com.us.usws.site.ws.entity.video.videoinfo.WSVideoInfoRecommend;
import com.us.usws.site.ws.entity.video.videoinfo.WSVideoInfoUser;
import com.us.usws.site.ws.entity.video.videoinfo.WSVideoInfoVideo;
import com.us.usws.site.ws.entity.video.videoinfo.WSVideoInfoVideoAttr;
import com.us.usws.site.ws.entity.video.videoinfo.WSVideoInfoVideoCat;
import com.us.usws.support.ext.CacheManager;
import com.us.usws.support.uitls.ServiceFactory;

public class PackEntityConverter {

	//private static Log log = LogFactory.getFactory().getInstance(PackEntityConverter.class);

	public static WSVideoInfoVideo genWSVideoInfoVideo(Video v) {

		WSVideoInfoVideo ret = new WSVideoInfoVideo();

		ret.setUid(v.getUid());
		ret.setName(v.getName());
		ret.setDescription(v.getDescription());
		ret.setPlayLen(v.getVideoFile().getPlayLen());
		ret.setPlayCnt(v.getPlayCnt());

		ret.setRateStarNum(VideoRateStartWorker.getRateStar(v.getPlayCnt()));
		ret.setVideoInfoUrl(WSResourceURIReplacer.getVideoInfoURL(v.getUid()));
		ret.setSnapshot(WSResourceURIReplacer.getVideoSnapshotURL(v.getVideoFile().getUid(), v.getVideoFile().getSnapshot()));

		//video file
		ret.setVideoFileUrl(WSResourceURIReplacer.getVideoFileURL(v.getVideoFile().getUid(), v.getVideoFile().getFileExt()));

		//categories
		if (v.getCategories() != null) {
			List<WSVideoInfoVideoCat> categories = new ArrayList<WSVideoInfoVideoCat>();
			for (BaseVideoCat c : v.getCategories()) {
				categories.add(genOneVideoCat(c));
			}
			ret.setCategories(categories);
		}

		//attributes
		if (v.getExtraAttributes() != null) {
			List<WSVideoInfoVideoAttr> attributes = new ArrayList<WSVideoInfoVideoAttr>();
			for (VideoExtattr e : v.getExtraAttributes()) {
				String euid = e.getExtattrUid();
				String elabel = CacheManager.getVideoTypeExtattrCache().getLabel(euid);
				attributes.add(new WSVideoInfoVideoAttr(elabel, e.getValue()));
			}
			ret.setAttributes(attributes);
		}

		//user
		UserROService service = ServiceFactory.getInstance().getService(UserROService.class);
		WSVideoInfoUser user = new WSVideoInfoUser();

		String createByUid = v.getCreateByUid();
		User creator = service.getUser(createByUid);
		user.setName(creator.getFullName());
		user.setAvatar(WSResourceURIReplacer.getUserAvatar(creator.getUid(), creator.getAvatar()));
		user.setUrl(WSResourceURIReplacer.getUserUrl(creator.getUid()));
		ret.setUser(user);

		//comments(votes0
		WSVideoInfoComment comments = new WSVideoInfoComment();
		comments.setVoteBadCnt(v.getVoteBadCnt());
		comments.setVoteGoodCnt(v.getVoteGoodCnt());
		comments.setVoteListUrl(WSResourceURIReplacer.getVideoCommentUrl(v.getUid()));

		//recommendations
		List<WSVideoInfoRecommend> recommends = new ArrayList<WSVideoInfoRecommend>();
		if (v.getRecommands() != null) {
			for (DOVideo ev : v.getRecommands()) {
				recommends.add(genOneRecommendVideo(ev));
			}

			//pagination
			DOVideo prevVideo = v.getRecommands().get(0);
			ret.setPrevVideoUrl(WSResourceURIReplacer.getVideoInfoURL(prevVideo.getUid()));
			DOVideo nextVideo = v.getRecommands().get(v.getRecommands().size() - 1);
			ret.setNextVideoUrl(WSResourceURIReplacer.getVideoInfoURL(nextVideo.getUid()));
		}
		ret.setRecommendations(recommends);

		//comments
		WSVideoInfoComment wscmt = new WSVideoInfoComment();
		wscmt.setVoteBadCnt(v.getVoteBadCnt());
		wscmt.setVoteGoodCnt(v.getVoteGoodCnt());
		wscmt.setVoteListUrl(WSResourceURIReplacer.getVideoCommentUrl(v.getUid()));
		ret.setComments(wscmt);

		//return
		return ret;
	}

	/**
	 * get one recommend video
	 * @param v
	 * @return
	 */
	private static WSVideoInfoRecommend genOneRecommendVideo(DOVideo v) {
		WSVideoInfoRecommend ret = new WSVideoInfoRecommend();
		ret.setName(v.getName());
		ret.setUid(v.getUid());

		//play count
		VideoPlayLogROService vpservice = ServiceFactory.getInstance().getService(VideoPlayLogROService.class);
		int playCnt = vpservice.getPlayCnt(v.getUid());
		ret.setRateStarNum(VideoRateStartWorker.getRateStar(playCnt));

		//video file
		VideoFileROService vfservice = ServiceFactory.getInstance().getService(VideoFileROService.class);
		VideoFile vf = vfservice.getVideoFile(v.getFileUid());
		ret.setSnapshot(WSResourceURIReplacer.getVideoSnapshotURL(v.getFileUid(), vf.getSnapshot()));
		return ret;
	}

	/**
	 * private get video cat
	 * @param cat
	 * @return
	 */
	private static WSVideoInfoVideoCat genOneVideoCat(DOBaseVideoCat cat) {
		WSVideoInfoVideoCat ret = new WSVideoInfoVideoCat();
		ret.setName(cat.getName());
		ret.setUid(cat.getUid());
		ret.setUrl(WSResourceURIReplacer.getCategoryVideoListURL(cat.getUid()));
		return ret;
	}

	/**
	 * get video comment
	 */
	public static WSVideoCommentDetail genWSVideoCommentDetail(VideoComment eachC) {
		//prepare services
		UserROService uservice = ServiceFactory.getInstance().getService(UserROService.class);
		//VideoCommentROService cservice = ServiceFactory.getInstance().getService(VideoCommentROService.class);

		WSVideoCommentDetail ret = new WSVideoCommentDetail();
		ret.setCreateDate(eachC.getCreateDate());
		ret.setDescription(eachC.getDescription());
		ret.setLastUpdDate(eachC.getLastUpdDate());
		ret.setRateScore(eachC.getRateScore() > 0 ? 1 : 0);
		ret.setUid(eachC.getUid());

		//user
		DOUser user = uservice.getDOUser(eachC.getUserUid());

		WSVideoCommentUser u = new WSVideoCommentUser();
		u.setAvatar(WSResourceURIReplacer.getUserAvatar(user.getUid(), user.getAvatar()));
		u.setName(user.getFullName());
		u.setUrl(WSResourceURIReplacer.getUserUrl(user.getUid()));

		//	List<VideoComment> comments=eachC.g

		return ret;
	}
}
